<?php
Yaf_loader::import("SDK/getui/IGt.Push.php");
$tConf = Yaf_Registry::get("config")->getui->default;
define('APPKEY',$tConf['appkey']);
define('APPID',$tConf['appid']);
define('MASTERSECRET',$tConf['mastersecret']);
define('HOST','http://sdk.open.api.igexin.com/apiex.htm');
define('taskId','OSS-0213_UA9Aq1pG1W97zj1zJ3q0e5');

$pCid = empty($_GET['mem_cid'])?'':trim($_GET['mem_cid']);
$pDevicetoken = empty($_GET['mem_devicetoken'])?'':trim($_GET['mem_devicetoken']);
$pTitle = empty($_GET['title'])?'':trim($_GET['title']);
$pNr = empty($_GET['nr'])?'':trim($_GET['nr']);
$pUrl = empty($_GET['url'])?'':trim($_GET['url']);
$pId = empty($_GET['id'])?0:trim($_GET['id']);
$pType = empty($_GET['type'])?0:trim($_GET['type']);# 1解读成功 2解读失败 3化验单问题反馈 4个人中心意见反馈 5健康新闻 6健康评测

define('CID',$pCid);
define('DEVICETOKEN',$pDevicetoken);
$nr = mb_strcut($pNr, 0, 85, 'utf-8');
define('title',$pTitle);
define('nr',$nr);
define('url',$pUrl);
define('id',$pId);
define('type',$pType);

class GetuinewController extends Ctrl_Base{
	protected $_auth = 1;

    //群发消息
    public function bulkAction(){
        $this->pushMessageToApp();
        exit; 
    }

    private function pushMessageToApp(){
        $igt = new IGeTui(HOST,APPKEY,MASTERSECRET);
        #$template = $this->IGtNotificationTemplateDemo();
        $template = $this->IGtTransmissionTemplateDemo();
        $message = new IGtAppMessage();
        $message->set_isOffline(true);
        $message->set_offlineExpireTime(3600*12*1000);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
        $message->set_data($template);
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        $message->set_speed(100);// 设置群推接口的推送速度，单位为条/秒，例如填写100，则为100条/秒。仅对指定应用群推接口有效。
        $message->set_appIdList(array(APPID));
        $message->set_phoneTypeList(array('ANDROID'));
        $rep = $igt->pushMessageToApp($message);
        var_dump($rep);
        echo ("<br><br>");


        $igt = new IGeTui(HOST,APPKEY,MASTERSECRET);
        $template = $this->IGtTransmissionTemplateDemo();
        $message = new IGtAppMessage();
        $message->set_isOffline(true);
        $message->set_offlineExpireTime(3600*12*1000);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
        $message->set_data($template);
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        $message->set_speed(100);// 设置群推接口的推送速度，单位为条/秒，例如填写100，则为100条/秒。仅对指定应用群推接口有效。
        $message->set_appIdList(array(APPID));
        $message->set_phoneTypeList(array('IOS'));
        $rep = $igt->pushMessageToApp($message);
        var_dump($rep);
        echo ("<br><br>");
    }

	private function IGtNotificationTemplateDemo(){
        $template =  new IGtNotificationTemplate();
        $template->set_appId(APPID);//应用appid
        $template->set_appkey(APPKEY);//应用appkey
        $template->set_transmissionType(2);//透传消息类型
        $template->set_transmissionContent("url=".url."&title=".title.'&nr='.nr."&type=".type."&id=".id);//透传内容
        return $template;
	}
    private function IGtTransmissionTemplateDemo(){
        $template =  new IGtTransmissionTemplate();
        $template->set_appId(APPID);//应用appid
        $template->set_appkey(APPKEY);//应用appkey
        $template->set_transmissionType(2);//透传消息类型
        $template->set_transmissionContent("url=".url."&title=".title.'&nr='.nr."&type=".type."&id=".id);//透传内容
		return $template;
    }
    //单推
    public function singleAction(){
    
		$igt = new IGeTui('',APPKEY,MASTERSECRET);

		$template = $this->touchuandemo();
		$message = new IGtSingleMessage();
		$message->set_isOffline(true);//
		$message->set_offlineExpireTime(3600*12*1000);//
		$message->set_data($template);//

		$target = new IGtTarget();
		$target->set_appId(APPID);
		$target->set_clientId(CID);
		$rep = $igt->pushMessageToSingle($message,$target);
		var_dump($rep);
		echo ("<br><br>");
        exit;
    }
    private function touchuandemo(){
        $template =  new IGtTransmissionTemplate();
        $template->set_appId(APPID);//应用appid
        $template->set_appkey(APPKEY);//应用appkey
        $template->set_transmissionType(2);//透传消息类型
        $template->set_transmissionContent("url=".url."&title=".title.'&nr='.nr."&type=".type."&id=".id);//透传内容
        #$template->set_pushInfo("actionLocKey","badge","message","sound","payload","locKey","locArgs","launchImage");

        $apn = new IGtAPNPayload();

        $alertmsg=new SimpleAlertMsg();
        $alertmsg->alertMsg="";
        $apn->alertMsg=$alertmsg;
        $apn->badge=2;
        $apn->sound="";
        $apn->add_customMsg("payload","payload");
#        $apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);

		return $template;
    }
    public function touchuaniosAction(){
            
		$template = $this->touchuandemo();
        $message = new IGtSingleMessage();

        $igt = new IGeTui(HOST,APPKEY,MASTERSECRET);
        $message->set_data($template);
        $ret = $igt->pushAPNMessageToSingle(APPID, DEVICETOKEN, $message);
        var_dump($ret);
        exit;
	}
}
